/*
 * clithread.c
 *
 *  Created on: Apr 29, 2012
 *      Author: root
 */


#include	"unp.h"

void str_cli(FILE *fp, int sockfd) {
	int maxfdp1, stdineof, toggle = 0;
	fd_set rset;
	char buf[MAXLINE], buf1[MAXLINE];
	char message[MAXLINE];
	int n;
	char *command;
	char *debug_msg;
	char myid[MAXLINE], youid[MAXLINE];

	stdineof = 0;
	FD_ZERO(&rset);
	//printf(">");
	//fflush(stdout);
	for (;;) {

		if (stdineof == 0)
			FD_SET(fileno(fp), &rset);
		FD_SET(sockfd, &rset);
		maxfdp1 = max(fileno(fp), sockfd) + 1;
		Select(maxfdp1, &rset, NULL, NULL, NULL);
		if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
			if ((n = Read(sockfd, buf, MAXLINE)) == 0) {
				if (stdineof == 1)
					return; /* normal termination */
				else
					err_quit("str_cli: server terminated prematurely");
			}
			sprintf(buf1,"%s",buf);
			debug_msg=strtok(buf1," ");

			if(toggle==0) //debug off don't display request, response message
			{
			if(!strcmp(buf1,"299")||!strcmp(buf1,"230")||!strcmp(buf1,"200")||!strcmp(buf1,"221"))
			{}
			else
			{
			Write(fileno(stdout), buf, n);
			//printf(">");
			//fflush(stdout);
			}
			}


			if(toggle==1) // debug on , display request ,response message
			{
			if(!strcmp(buf1,"299")||!strcmp(buf1,"230")||!strcmp(buf1,"200")||!strcmp(buf1,"221"))
							{
				Write(fileno(stderr), buf, n);
				//printf(">");
				//fflush(stdout);
							}
							else
							{

							}
			}

			memset(buf, 0, strlen(buf));
			memset(message, 0, strlen(message));
		}

		if (FD_ISSET(fileno(fp), &rset)) {
			/* input is readable */
			if ((n = Read(fileno(fp), buf, MAXLINE)) == 0) {
				stdineof = 1;
				Shutdown(sockfd, SHUT_WR); /* send FIN */
				FD_CLR(fileno(fp), &rset);
				continue;
			}

			crlf_add(buf, MAXLINE, buf, n);

			if (!strcmp("who\r\n", buf)) {
				sprintf(buf, "WHO\r\n\r\n");
			}

			if (!strcmp("quit\r\n", buf)) {
				printf("***************\n");
				sprintf(buf, "QUIT\r\n\r\n");
				Writen(sockfd,buf,strlen(buf));
				stdineof = 1;
				Shutdown(sockfd, SHUT_WR); /* send FIN */
				FD_CLR(fileno(fp), &rset);
				continue;
			}

			if (!strcmp("help\r\n", buf)) {
				printf("****************************************************\n");
				printf("----User [ID(my id)]'Enter' is Log in\n");
				printf("----who'Enter' is Logged User ID list\n");
				printf("----talk [ID(your id)]'Enter' is talk mode\n");
				printf("----In the talk mode, [message] typing\n");
				printf("----In the talk mode, .'Enter'  is End of talk mode\n");
				printf("----quit'Enter' is Close the chatting\n");
				printf("****************************************************\n");

			}

			if (!strcmp("debug\r\n", buf)) {
				if (toggle == 0)
					toggle = 1;
				else
					toggle = 0;
			}

			command = strtok(buf, " ");
			if (!strcmp("User", buf)) {
				command = strtok(NULL, " \r\n\r\n");
				sprintf(myid, "%s", command);
				sprintf(buf, "USER %s\r\n\r\n", command);
			}

			if (!strcmp("talk", buf)) {
				printf("----------------------talk mode------------------\n");
				command = strtok(NULL, "\n");
				sprintf(youid, "%s", command);

				printf("%s:", myid);
				fflush(stdout);

				for (;;) {

					if (stdineof == 0)
						FD_SET(fileno(fp), &rset);
					FD_SET(sockfd, &rset);
					maxfdp1 = max(fileno(fp), sockfd) + 1;
					Select(maxfdp1, &rset, NULL, NULL, NULL);
					if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
						if ((n = Read(sockfd, buf, MAXLINE)) == 0) {
							if (stdineof == 1)
								return; /* normal termination */
							else
								err_quit(
										"str_cli: server terminated prematurely");
						}


						sprintf(buf1,"%s",buf);
						debug_msg=strtok(buf1," ");

						if(toggle==0) //debug off don't display request, response message
						{
						if(!strcmp(buf1,"299")||!strcmp(buf1,"200"))
						{}
						else
						{
						Write(fileno(stdout), buf, n);
						printf("%s:", myid);
						fflush(stdout);
						}
						}

						if(toggle==1) // debug on , display request ,response message
						{
							if(!strcmp(buf1,"299")||!strcmp(buf1,"200"))
							{
								Write(fileno(stderr), buf, n);
								printf("%s:", myid);
								fflush(stdout);
							}
							else
							{

							}
						}


						memset(buf, 0, strlen(buf));
						memset(buf1,0,strlen(buf1));
					}

					if (FD_ISSET(fileno(fp), &rset)) {
						/* input is readable */
						if ((n = Read(fileno(fp), message, MAXLINE)) == 0) {
							stdineof = 1;
							Shutdown(sockfd, SHUT_WR); /* send FIN */
							FD_CLR(fileno(fp), &rset);
							continue;
						}
						if (!strcmp(message, ".\n")) {
							printf("----------------------close talk mode--------------------\n");
							break;
						}
						crlf_add(message, MAXLINE, message, n);

						sprintf(buf, "FROM %s TO %s\r\n%s\r\n", myid, youid,
								message);
						Writen(sockfd, buf, strlen(buf));
						memset(buf, 0, strlen(buf));
						memset(message, 0, strlen(message));

					}
				}

			}

			if (toggle == 1) {
					Write(fileno(stderr),buf,n);
				}

				Writen(sockfd, buf, strlen(buf));
				memset(buf, 0, strlen(buf));
				memset(buf1, 0, strlen(buf1));


		}

	}
}


